const state = {
	// 用户列表
	users: [],
	// 商家列表
	merchants: [],
	// 活动列表
	activities: [],
}

const getters = {
	getActivitiesId(state) {
		return (id) => {
			const filter = state.activities.filter(item => item.activityId == id)[0];
			// console.log(`指定id=${ id }数据：${JSON.stringify(filter)}`)
			return filter
		}
	},
}

const mutations = {
	//获取用户数据
	getUsersData(state) {
		uni.request({
			url: 'http://localhost:3000/admin/users',
			method: 'get',
			success: (res) => {
				state.users = res.data
				console.log("获取数据：", res);
			},
			fail: (res) => {
				console.log(res)
			}
		})
	},
	//获取商家数据
	getMerchantsData(state) {
		uni.request({
			url: 'http://localhost:3000/admin/merchants',
			method: 'get',
			success: (res) => {
				state.merchants = res.data
				console.log(res);
			},
			fail: (res) => {
				console.log(res)
			}
		})
	},
	getActivities(state) {
		uni.request({
			url: 'http://localhost:3000/admin/activities',
			method: 'get',
			success: (res) => {
				state.activities = res.data
				console.log(res);
			},
			fail: (res) => {
				console.log(res)
			}
		})
	},
	//更改头像
	changeImage(state, obj) {
		uni.chooseImage({
			count: 1, // 默认9
			sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
			success: (res) => {
				const tempFilePaths = res.tempFilePaths;
				uni.uploadFile({
					url: 'http://localhost:3000/images', // 替换为你的Node.js后端地址
					filePath: tempFilePaths[0],
					name: 'file',
					success: (uploadRes) => {
						// console.log(JSON.parse(uploadRes.data).filePath)
						let images = 'http://localhost:3000' + JSON.parse(uploadRes.data).filePath
						console.log("images：", images)
						obj.image = images
						console.log('上传成功:', JSON.parse(uploadRes.data));
						uni.request({
							url: 'http://localhost:3000/admin/updata',
							method: 'post',
							data: obj,
							success: (res) => {
								console.log("更新成功")
								console.log("res:", res)
							}
						});
					},
					fail: (err) => {
						console.error('上传失败:', err);
					}
				});

			}
		});
	},
	// 更新数据
	updata(state, data) {
		// console.log("state:", JSON.stringify(state))
		uni.request({
			url: 'http://localhost:3000/admin/updata',
			method: 'post',
			data: data,
			success: (res) => {
				console.log("更新成功")
				console.log("res:", res)
			}
		});
	},

}

const actions = {}

export default {
	namespaced: true,
	state,
	getters,
	mutations,
	actions,
}